package com.cac.demo.service.mspartshelvesbasic;

import com.cac.demo.dao.mspartshelvesbasic.MsPartShelvesBasicDao;
import com.cac.demo.entity.mspartshelvesbasic.MsPartShelvesBasicDTO;
import com.cac.demo.util.adapter.DaoException;
import com.cac.demo.util.adapter.PojoUtil;
import com.cac.demo.util.adapter.QueryReqBean;
import com.cac.demo.util.adapter.QueryRespBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MsPartShelvesBasicService {


    @Autowired
    private MsPartShelvesBasicDao msPartShelvesBasicDao;


    Date date = new Date();
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowTime = sf.format(date);
    Date time = sf.parse(nowTime);

    public MsPartShelvesBasicService() throws ParseException {
    }


    //大件货物分页查询
    @Transactional
    public QueryRespBean<HashMap> searchMsPartShelvesBasicByPage(QueryReqBean<HashMap> queryReqBean, String orderBy, String Keywords) {
        QueryRespBean<HashMap> queryRespBean = new QueryRespBean<>();
        try {
            PageHelper.startPage(queryReqBean.getPageParameter());
            HashMap searchParams = queryReqBean.getSearchParams();
            Page<HashMap> dataList = msPartShelvesBasicDao.searchMsPartShelvesBasicByPage(searchParams,orderBy,Keywords);
            queryRespBean.setResult(dataList);
            return queryRespBean;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }
    //判断查询是否为空
    @Transactional
    public List<HashMap> searchMsPartShelvesBasicByPage(HashMap searchParams, String orderBy, String Keywords) {
        try {
            Page<HashMap> dataList = msPartShelvesBasicDao.searchMsPartShelvesBasicByPage(searchParams,orderBy,Keywords);
            return dataList;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }
    //单条插入
    @Transactional
    public int insertMsPartShelvesBasic(Map map){
        try{
            if(!"".equals(map.get("locatorNo"))){
                int count = msPartShelvesBasicDao.insertMsPartShelvesBasicDTO(map);
                return count;
            }else {
                return 0;
            }

        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }


    //单条更新
    @Transactional
    public int updateMsPartBasic(Map map){
        try{
            int count = msPartShelvesBasicDao.updateMsPartShelvesBasic(map);
            if(count==0){
                throw new DaoException("修改数据失败");
            }
            return count;
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }



    //自定义方法，获取需要修改的dto对象
    private HashMap getUpdateDto(MsPartShelvesBasicDTO msPartShelvesBasicDTO){
        HashMap oldDto = findById(msPartShelvesBasicDTO.getMsPartShelvesId());
        if(oldDto == null ){
            throw new DaoException("数据不存在");
        }
        PojoUtil.copyProperties(oldDto,msPartShelvesBasicDTO,true);
        return oldDto;
    }


    private HashMap findById(String wmsLocatorId){
        return msPartShelvesBasicDao.selectByMsPartShelvesId(wmsLocatorId);
    }


    //删除
    @Transactional
    public int deleteMsPartShelvesBasic(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new DaoException("删除失败！传入的参数主键为null");
        }
        try {
            //记录日志
            HashMap msPartShelvesBasicDTO = msPartShelvesBasicDao.selectByMsPartShelvesId(id);
            if (msPartShelvesBasicDTO == null) {
                throw new DaoException("删除失败！对象不存在");
            }
            //删除业务数据
            int count = msPartShelvesBasicDao.deleteMsPartShelvesBasic(id);
            return count;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    //批量删除
    @Transactional
    public int deleteMsPartShelvesBasicByIds(String[] ids) {
        int result = 0;
        for (String id : ids) {
            deleteMsPartShelvesBasic(id);
            result++;
        }
        return result;
    }
}
